package com.whsxt.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whsxt.constant.CategoryConstant;
import com.whsxt.domain.Category;
import com.whsxt.mapper.CategoryMapper;
import com.whsxt.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-15 14:19
 * @Description: ${Description}
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "com.whsxt.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 分页查询商品的分类
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_PREFIX)
    public List<Category> findCategoryList() {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .orderByDesc(Category::getUpdateTime));
        return categoryList;
    }


    /**
     * 查询商品分类的所有一级节点
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_ROOT_PREFIX)
    public List<Category> findRootCategory() {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, 0L)
                .orderByDesc(Category::getUpdateTime));
        return categoryList;
    }


    /**
     * 新增商品分类
     *
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = CategoryConstant.CATEGORY_ROOT_PREFIX),
            @CacheEvict(key = CategoryConstant.CATEGORY_PREFIX)
    })                              //删除多个
    //@CacheEvict(key = CategoryConstant.CATEGORY_ROOT_PREFIX)            //删除一个
    public boolean save(Category category) {
        log.info("新增商品分类：{}", JSON.toJSONString(category));
        category.setRecTime(new Date());
        category.setUpdateTime(new Date());
        category.setShopId(1L);
        //新增父节点还是子节点
        Long parentId = category.getParentId();
        if (ObjectUtils.isEmpty(parentId)) {
            //父节点
            category.setParentId(0L);
        }

        return categoryMapper.insert(category) > 0;
    }


    /**
     * 根据父节点查询分类
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Category> findCategoryListById(Long parentId) {
        if (parentId == null) {
            parentId = 0L;
        }
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId)
        );
    }
}
